package com.linkgie.galaxyframework.metadata.structure;

import javax.validation.constraints.NotBlank;

import com.linkgie.galaxyframework.metadata.DataEntity;
import com.linkgie.galaxyframework.metadata.DataField;
import com.linkgie.galaxyframework.metadata.DataInterface;
import com.linkgie.galaxyframework.metadata.DataType;
import com.linkgie.galaxyframework.metadata.DefaultBoolean;
import com.linkgie.galaxyframework.metadata.DefaultType;
import com.linkgie.galaxyframework.metadata.ModelField;
import com.linkgie.galaxyframework.metadata.StandardType;

/**
 * 模型字段元数据；
 * 
 * @author huanghaiquan
 *
 */
@DataInterface
@DataEntity
public class ModelFieldMetadata extends FieldMetadata implements ModelField, ModelFieldBaseSettings {

	@DefaultType(StandardType.TEXT)
	@NotBlank
	@DataField(name = "类型", order = 10)
	private StandardType type;

	@DefaultBoolean(false)
	@NotBlank
	@DataType(contentType = "enum;strict=true;true='数组';false='单项'")
	@DataField(name = "重复性", order = 11)
	private boolean iterable;

	@DefaultBoolean(false)
	@NotBlank
	@DataType(contentType = "enum;strict=true;true='是';false='否'")
	@DataField(name = "必填", order = 12)
	private boolean required;
	
	
	@DefaultBoolean(false)
	@DataType(contentType = "enum;strict=true;true='是';false='否'")
	@DataField(name = "状态化", order = 30)
	private boolean stateful;
	
	@DataField(name = "默认状态", order = 31)
	private String defaultState;
	
	/**
	 * 状态判断条件；<p>
	 * 
	 * 如果未设置，则直接采用字段的值作为判断；
	 */
	@DataField(name = "状态断言", order = 32)
	private StatePredicateMetadata[] statesPredicates;

	@DataField(name = "状态绑定", order = 33)
	private StateBindingMetadata stateBinding;

	// TODO: 引用内容类型对象；
	@DataField(name = "内容格式", order = 120)
	private String contentType;

	@DataField(name = "聚合关系", order = 140)
	private AggregateMetadata[] aggregates;
	
	@DataField(name = "引用关系", order = 160)
	private ReferenceMetadata reference;
	
	@DataField(name = "组合关系", order = 180)
	private CompositeMetadata composite;


//	private DataRule[] rules;

	/**
	 * 默认值是针对 {@link StandardType} 数据类型中除了 {@link StandardType#OBJECT} 之外的类型字段而设置；
	 */
	private Object defaultValue;

	public ModelFieldMetadata() {
	}

	public ModelFieldMetadata(FieldMetadata fieldMetadata) {
		this.setKey(fieldMetadata.getKey());
		this.setName(fieldMetadata.getName());
		this.setModifiable(fieldMetadata.isModifiable());
		this.setHide(fieldMetadata.isHide());
		this.setHint(fieldMetadata.getHint());
		this.setReadonly(fieldMetadata.isReadonly());
		this.setOrder(fieldMetadata.getOrder());
	}

	@Override
	public StandardType getType() {
		return type;
	}

	@Override
	public void setType(StandardType type) {
		this.type = type;
	}

	@Override
	public boolean isIterable() {
		return iterable;
	}

	@Override
	public void setIterable(boolean iterable) {
		this.iterable = iterable;
	}

	@Override
	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

//	@Override
//	public DataRule[] getRules() {
//		return rules;
//	}
//
//	public void setRules(DataRule[] rules) {
//		this.rules = rules;
//	}

	@Override
	public Object getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(Object defaultValue) {
		this.defaultValue = defaultValue;
	}

	@Override
	public boolean isRequired() {
		return required;
	}

	@Override
	public void setRequired(boolean required) {
		this.required = required;
	}

	@Override
	public AggregateMetadata[] getAggregates() {
		return aggregates;
	}

	public void setAggregates(AggregateMetadata[] aggregates) {
		this.aggregates = aggregates;
	}

	public boolean isStateful() {
		return stateful;
	}

	public void setStateful(boolean stateful) {
		this.stateful = stateful;
	}

	public String getDefaultState() {
		return defaultState;
	}

	public void setDefaultState(String defaultState) {
		this.defaultState = defaultState;
	}

	public StatePredicateMetadata[] getStatesPredicates() {
		return statesPredicates;
	}

	public void setStatesPredicates(StatePredicateMetadata[] statesPredicates) {
		this.statesPredicates = statesPredicates;
	}

	public StateBindingMetadata getStateBinding() {
		return stateBinding;
	}

	public void setStateBinding(StateBindingMetadata stateBinding) {
		this.stateBinding = stateBinding;
	}

	@Override
	public CompositeMetadata getComposite() {
		return composite;
	}

	public void setComposite(CompositeMetadata composite) {
		this.composite = composite;
	}

	@Override
	public ReferenceMetadata getReference() {
		return reference;
	}

	public void setReference(ReferenceMetadata reference) {
		this.reference = reference;
	}

}
